package com.spr.gw.controller;

import com.spr.gw.common.ResultBean;
import com.spr.gw.entity.Category;
import com.spr.gw.entity.Message;
import com.spr.gw.entity.SystemConfig;
import com.spr.gw.entity.VO.MessageVo;
import com.spr.gw.service.CategoryService;
import com.spr.gw.service.MessageService;
import com.spr.gw.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * Created by lsy
 * on 2017/10/27 9:52.
 */
@Controller
@Slf4j
public class FrontController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private CategoryService categoryService;

    //配置欢迎界面
    @RequestMapping("/")
    public String welcome(){
        return "forward:/index.html";
    }

    @RequestMapping("/index.html")
    public ModelAndView index(ModelAndView mv){
        ResultBean<SystemConfig> systemConfig = systemConfigService.findSystemConfig();
        mv.addObject("config", systemConfig.getData());
        //查询首页大图
        List<MessageVo> pictures = messageService.findAllMessageByCategoryId(11);
        pictures = pictures.stream().limit(4).collect(toList());
        mv.addObject("pictures", pictures);
        //查询知识课堂内容
        List<MessageVo> knowledgeMessages = messageService.findAllMessageByCategoryId(27);
        mv.addObject("knowledgeMessages",knowledgeMessages);
        //查询产品中心下的内容
        List<MessageVo> children1 = messageService.findAllMessageByCategoryId(21);
        mv.addObject("children1",children1.stream().limit(10).collect(toList()));
        List<MessageVo> children2 = messageService.findAllMessageByCategoryId(22);
        mv.addObject("children2",children2.stream().limit(3).collect(toList()));
        List<MessageVo> children3 = messageService.findAllMessageByCategoryId(23);
        mv.addObject("children3",children3.stream().limit(3).collect(toList()));
        mv.setViewName("index");
        return mv;
    }

    //关于我们 --> 【多】个子类+【单】个内容
    @RequestMapping("/about.html")
    public ModelAndView about(ModelAndView mv, @RequestParam(required = false,name = "activeId",defaultValue = "2") String activeId){
        mv.addObject("activeId", activeId);
        //查询“关于我们”下面的子类 关于我们id=>1
        List<Category> categories = categoryService.findChildCategoryByPid(1);
        mv.addObject("categories", categories);
        setInnerBanner(mv);
        mv.setViewName("about");
        return mv;
    }

    //新闻中心 --> 【多】个子类+分页
    @RequestMapping("/news.html")
    public ModelAndView newsCenter(ModelAndView mv, @RequestParam(required = false,name = "activeId",defaultValue = "18") String activeId){
        mv.addObject("activeId", activeId);
        List<Category> categories = categoryService.findChildCategoryByPid(17);
        mv.addObject("categories", categories);
        setInnerBanner(mv);
        mv.setViewName("news");
        return mv;
    }

    //产品中心  --> 【多】个子类+分页
    @RequestMapping("/product.html")
    public ModelAndView productCenter(ModelAndView mv, @RequestParam(required = false,name = "activeId",defaultValue = "21") String activeId){
        mv.addObject("activeId", activeId);
        List<Category> categories = categoryService.findChildCategoryByPid(20);
        mv.addObject("categories", categories);
        setInnerBanner(mv);
        mv.setViewName("product");
        return mv;
    }

    //加盟代理 --> 单页
    @RequestMapping("/jiameng.html")
    public ModelAndView jiamengProxy(ModelAndView mv){
        //单页【加盟代理】,categoryId => 25
        Message message = messageService.findOnePageContent(25);
        mv.addObject("message", message);
        setInnerBanner(mv);
        mv.setViewName("jiameng");
        return mv;
    }

    //知识课堂 --> 一个子类+分页
    @RequestMapping("/knowledge.html")
    public ModelAndView knowledgeClass(ModelAndView mv){
        //直接跳转页面，把内容传递过去
        //【知识课堂】,categoryId => 27
        List<MessageVo> messages = messageService.findAllMessageByCategoryId(27);
        mv.addObject("messages",messages);
        setInnerBanner(mv);
        mv.setViewName("knowledge");
        return mv;
    }

    //招贤纳士 --> 一个子类+分页
    @RequestMapping("/recruit.html")
    public ModelAndView recruitPeoson(ModelAndView mv){
        //直接跳转页面，把内容传递过去
        //【招纳贤士】,categoryId => 29
        List<MessageVo> messages = messageService.findAllMessageByCategoryId(29);
        mv.addObject("messages",messages);
        setInnerBanner(mv);
        mv.setViewName("recruit");
        return mv;
    }

    //联系我们 --> 单页
    @RequestMapping("/contact.html")
    public ModelAndView contactUs(ModelAndView mv){
        //单页【联系我们】,categoryId => 31
        Message message = messageService.findOnePageContent(31);
        mv.addObject("message", message);
        setInnerBanner(mv);
        mv.setViewName("contact");
        return mv;
    }

    //内容详情
    @RequestMapping("/detail.html/{messageId}")
    public ModelAndView articleDetail(ModelAndView mv, @PathVariable("messageId") String messageId){
        Message message = messageService.findOneMessage(Integer.valueOf(messageId));
        if(message == null ){
            mv.setViewName("index");
            return mv;
        }
        Category category = categoryService.findCategoryByPrimaryKey(message.getCategoryId());
        //浏览量+1
        if(message.getClick()==null)
            message.setClick(0);
        message.setClick(message.getClick()+1);
        messageService.insertMessage(message);
        mv.addObject("message", message);
        mv.addObject("pid", category.getPid());
        setInnerBanner(mv);
        mv.setViewName("detail");
        return mv;
    }

    private void setInnerBanner(ModelAndView mv){
        //查询首页大图
        List<MessageVo> pictures = messageService.findAllMessageByCategoryId(12);
        pictures = pictures.stream().limit(4).collect(toList());
        mv.addObject("pictures", pictures);
    }
}
